﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Softserve.NumberToWords.Abstract;

namespace Softserve.NumberToWords.Concrete
{
    /// <summary>
    /// Working converter from number to words
    /// </summary>
    class NumberToWordsConverter
    {

        public ConvertDictionaryBase InternalDictionary { get; set; }
        private List<byte> mParsedNumber = default(List<byte>);
        public NumberToWordsConverter(ConvertDictionaryBase dictionary)
        {
            InternalDictionary = dictionary;
        }


        #region Public Logic
        /// <summary>
        /// Convert from number to words
        /// </summary>
        /// <param name="number">Positive int</param>
        /// <returns>String that corresponds by pronousiation for int number in arguments</returns>
        public string Convert(int number)
        {
            Parse(number);
            StringBuilder s = new StringBuilder();
            int i = mParsedNumber.Count;
            foreach (var item in mParsedNumber)
            {
                if (item == 0 && mParsedNumber.Count == 1)
                {
                    s.Append(InternalDictionary.GetTeenNumber(item));
                }
                else
                {
                    if (item <= 19)
                    {
                        s.Append(InternalDictionary.GetTeenNumber(item));
                    }
                    else
                    {
                        s.Append(InternalDictionary.GetDecimalNumber(item));
                    }
                }
                s.Append(InternalDictionary.GeneralTemplate);
                if (i > 1)
                {
                    if (i % 2 == 0)
                    {
                        s.Append(InternalDictionary.Teen[1]);
                    }
                    else
                    {
                        s.Append(InternalDictionary.Teen[(i + 1) / 2]);
                    }
                    s.Append(InternalDictionary.GeneralTemplate);
                }
                i--;
            }
            return s.ToString();

        } 
        #endregion

        private void Parse(int number)
        {
            mParsedNumber = new List<byte>();
            byte b = 2;
            int i = 0;
            byte beginOfNumbers = 48;
            StringBuilder s = new StringBuilder(number.ToString());
            while (s.Length > 1)
            {
                if (++i % 2 == 0)
                {
                    b = (byte)(s[s.Length - 1] - beginOfNumbers);
                    s.Remove(s.Length - 1, 1);
                }
                else
                {
                    b = (byte)(10 * (s[s.Length - 2] - beginOfNumbers) + s[s.Length - 1] - beginOfNumbers);
                    s.Remove(s.Length - 2, 2);
                }
                mParsedNumber.Add(b);
            }
            if (s.Length > 0)
            {
                mParsedNumber.Add((byte)(s[0] - beginOfNumbers));
            }
            mParsedNumber.Reverse();
        }
    }
}
